package com.wzdigit.wms.wms.service.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.inventory.LotAttr;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusLotAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LotAttrResponse;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.basic.mapper.core.basic.LotAttrMapper;
import com.wzdigit.wms.basic.service.code.LotnoService;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 批次属性-服务类
 * @author yangdongjie
 * @LotAttrService.java
 * @2021年1月19日下午2:22:07
 */
@Service
public class LotAttrService {

	@Autowired
    private LotAttrMapper lotAttrMapper;
	
	@Autowired
	private LotnoService lotnoService;
	
	/**
	 * 批次属性列表搜索
	 * @param dto
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PagerInfo<List<LotAttrResponse>> searchLotAttr(SearchLotAttrRequest dto) {
		Page page = PageHelper.offsetPage(dto.getSkipCount(), dto.getMaxResultCount());
        PagerInfo<List<LotAttrResponse>> pagerInfo = new PagerInfo<List<LotAttrResponse>>();
        List<LotAttrResponse> list = lotAttrMapper.selectResponseAll(dto);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
	
	/**
	 * 批次属性详情
	 * @param lotAttrId
	 * @return
	 */
	public LotAttrResponse getLotAttr(Integer lotAttrId) {
		LotAttrResponse lotAttrResponse = lotAttrMapper.selectResponseByLotAttrId(lotAttrId);
		return lotAttrResponse;
    }
	
	/**
	 * 创建批次属性
	 * @param dto
	 * @param servletRequest
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public LotAttrResponse createLotAttr(CreateLotAttrRequest dto,
    		HttpServletRequest servletRequest) {
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		
		QueryWrapper<LotAttr> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("LOT_ATTR_CODE", dto.getLotAttrCode());
		LotAttr lotAttr = lotAttrMapper.selectOne(queryWrapper);
		if (lotAttr != null) {
			// 编码已存在
    		Shift.fatal(StatusCode.CODE_DATA_EXIST);
		}
		
		lotAttr = new LotAttr();
    	BeanUtil.copyNonNull(dto, lotAttr);
    	lotAttr.setCreateUser(loginName);
    	lotAttr.setCreateTime(new Date());
    	lotAttrMapper.insert(lotAttr);
    	
    	LotAttrResponse response = new LotAttrResponse();
    	BeanUtil.copyNonNull(lotAttr, response);
		return response;
    }
    
    /**
     * 编辑批次属性
     * @param dto
     * @param servletRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public LotAttrResponse updateLotAttr(UpdateLotAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	ifCanUpdate(dto.getLotAttrId());
		
		QueryWrapper<LotAttr> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("LOT_ATTR_CODE", dto.getLotAttrCode());
		LotAttr lotAttr = lotAttrMapper.selectOne(queryWrapper);
		if (lotAttr != null) {
			if(lotAttr.getLotAttrId().intValue() != dto.getLotAttrId().intValue()) {
				// 编码已存在
				Shift.fatal(StatusCode.CODE_DATA_EXIST);
			}
		}
		
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		
		lotAttr = lotAttrMapper.selectById(dto.getLotAttrId());
    	BeanUtil.copy(dto, lotAttr);
    	lotAttr.setModifyUser(loginName);
    	lotAttr.setModifyTime(new Date());
    	lotAttrMapper.updateById(lotAttr);
    	
    	LotAttrResponse response = new LotAttrResponse();
    	BeanUtil.copyNonNull(lotAttr, response);
		return response;
    }
    
    /**
     * 修改状态
     * @param dto
     * @param servletRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> updateStatus(UpdateStatusLotAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	ifCanUpdate(dto.getLotAttrId());
    	
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	
    	LotAttr lotAttr = new LotAttr();
    	lotAttr.setLotAttrId(dto.getLotAttrId());
    	lotAttr.setIsEnabled(dto.getIsEnabled());
    	lotAttr.setModifyUser(loginName);
    	lotAttr.setModifyTime(new Date());
    	lotAttrMapper.updateById(lotAttr);
		return new Result<>();
    }
    
	/**
	 * 判断批次属性是否可以编辑修改
	 * @param lotAttrId
	 */
	public void ifCanUpdate(Integer lotAttrId) {
		int size = lotnoService.selectCountByLotAttrId(lotAttrId);
		if (size > 0) {
			Shift.fatal(StatusCode.LOT_ATTR_USE);
		}
	}
}
